#pragma once


#include <opencv2/opencv.hpp> 
using namespace cv;

/// <summary>
/// 图片的算术运算
/// </summary>
/// <param name="image"></param>
void  operators_demo(Mat& image)
{
	Mat dst = Mat::zeros(image.size(), image.type());
	Mat dst2 = Mat::zeros(image.size(), image.type());

	imshow("原图", image);

#pragma region 一般方法

	dst = image + Scalar(50, 50, 50);
	imshow("调高亮度  一般加法操作", dst);
	dst = image - Scalar(50, 50, 50);
	imshow("调低亮度  一般减法操作", dst);
	dst = image / Scalar(50, 50, 50);
	imshow("调低亮度  一般除法操作", dst);

#pragma endregion

#pragma region 函数方法

	//乘法有特殊函数提供  亮度提高  至255截断  


	//新增原图同大小和类型的图像矩阵mat
	Mat m = Mat::zeros(image.size(), image.type());
	m = Scalar(2, 2, 2);


	//开始乘法  原图矩阵*新图像矩阵 = 需要显示的图像矩阵
	multiply(image, m, dst);
	imshow("内置方法的乘法操作", dst);

	//开始加法  原图矩阵+新图像矩阵 = 需要显示的图像矩阵
	add(image, m, dst);
	imshow("内置方法的加法操作", dst);

	//开始减法  原图矩阵-新图像矩阵 = 需要显示的图像矩阵
	subtract(image, m, dst);
	imshow("内置方法的减法操作", dst);

	//开始除法  原图矩阵/新图像矩阵 = 需要显示的图像矩阵
	divide(image, m, dst);
	imshow("内置方法的除法操作", dst);


#pragma endregion

#pragma region 像素乘法具体实现
	int w = image.cols;
	int h = image.rows;
	int dims = image.channels();
	for (int row = 0; row < h; row++)
	{
		for (int col = 0; col < w; col++)
		{
			//彩色图像
			if (dims == 3)
			{
				Vec3b p1 = image.at<Vec3b>(row, col);
				Vec3b p2 = m.at<Vec3b>(row, col);

				dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(p1[0] + p2[0]);
				dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(p1[1] + p2[1]);
				dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(p1[2] + p2[2]);
			}
		}

	}

	for (int row = 0; row < h; row++)
	{
		//得到当前行的指针地址
		uchar* current_row_p1 = image.ptr<uchar>(row);
		uchar* current_row_p2 = m.ptr<uchar>(row);
		uchar* current_row_p3 = dst2.ptr<uchar>(row);

		for (int col = 0; col < w; col++)
		{
			//彩色图像
			if (dims == 3)
			{
				/*		*current_row_p3++ = *current_row_p1++ * *current_row_p2++;
						*current_row_p3++ = *current_row_p1++ * *current_row_p2++;
						*current_row_p3++ = *current_row_p1++ * *current_row_p2++;*/

				*current_row_p3++ = saturate_cast<uchar>(*current_row_p1++ * *current_row_p2++);
				*current_row_p3++ = saturate_cast<uchar>(*current_row_p1++ * *current_row_p2++);
				*current_row_p3++ = saturate_cast<uchar>(*current_row_p1++ * *current_row_p2++);
			}
		}
	}
	imshow("自实现指针加法", dst2);
#pragma endregion

	//--------------------

}
